class CharactersController < ApplicationController
  before_filter :authorize_admin, :only => [ ]

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy ],
         :redirect_to => { :action => :show }

  def index
    list
    render :action => 'list'
  end

  def list
    @character_pages, @characters = paginate :characters, :per_page => 10
  end

  def show
    @character = Character.find(params[:id])
  end

  def new
    @character = Character.new
  end

  def create
    @character = Character.new(params[:character])
    if @character.save
      flash[:notice] = 'Character was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @character = Character.find(params[:id])
  end

  def update
    @character = Character.find(params[:id])
    if @character.update_attributes(params[:character])
      flash[:notice] = 'Character was successfully updated.'
      redirect_to :action => 'show', :id => @character
    else
      render :action => 'edit'
    end
  end

  # TODO: Modify to retire Characters instead of delete them if they have any missions attached
  def destroy
    regs = MissionRegistration.find_by_character_id(params[:id])
    if regs.nil?
      Character.find(params[:id]).destroy
    else
      flash[:notice] = 'Cannot destroy character that has registered for a mission'
    end
    redirect_to :controller => 'command', :action => 'index'
  end
end
